package com.haoran.dwz.web;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.haoran.dwz.service.SystemSetupService;
import com.haoran.dwz.service.UserService;

@Controller
public class SystemController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private SystemSetupService systemSetupService;

	@RequestMapping(value = "init", method = { RequestMethod.POST })
	public String bootstrapLoader(String username,String email,String password,String pwd) {
		if(systemSetupService.isSetup()) {
			return "redirect:login";
		}else if(StringUtils.equals(password, pwd)) {
			logger.info("init system root" );
			userService.createRootUser(password);
			return "redirect:login";
		}
		return "redirect:install";
	}
}
